<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio v-for="(type, index) in dataForm.typeList" :label="index" :key="index" @change="changeRadio">{{ type }}</el-radio>
        </el-radio-group>
      </el-form-item>
    <el-form-item label="模块名称" prop="moduleName">
      <el-input v-model="dataForm.moduleName" placeholder="模块名称"></el-input>
    </el-form-item>
    <el-form-item label="主模块" prop="parentId">
      <el-select v-model="dataForm.parentId" placeholder="请选择主模块" :disabled="moduleShow">
        <el-option
          v-for="item in moduleValue"
          :key="item.moduleId"
          :label="item.moduleName"
          :value="item.moduleId">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="模块图标" prop="moduleIcon">
      <el-input v-model="dataForm.moduleIcon" placeholder="模块图标"></el-input>
    </el-form-item>
    <el-form-item label="模块跳转" prop="moduleUrl">
      <el-input v-model="dataForm.moduleUrl" placeholder="web模块跳转地址"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        moduleShow: false,
        moduleValue: [],
        visible: false,
        dataForm: {
          type: 0,
          typeList: ['服务', '资讯', '主模块'],
          moduleId: 0,
          moduleName: '',
          parentId: '',
          moduleIcon: '',
          moduleUrl: '',
          moduleType: ''
        },
        dataRule: {
          moduleName: [
            { required: true, message: '模块名称不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '请选择相应的主模块', trigger: 'blur' }
          ],
          moduleIcon: [
            { required: true, message: '模块图标不能为空', trigger: 'blur' }
          ],
          moduleType: [
            { required: true, message: '请选择相应模块类型', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.moduleId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.$http({
            url: this.$http.adornUrl('/info/moduleconfigtenant/listFor'),
            method: 'get',
            params: this.$http.adornParams({
              'moduleType': '3'
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              console.log(data.list)
              this.moduleValue = data.list
            }
          })
          if (this.dataForm.moduleId) {
            this.$http({
              url: this.$http.adornUrl(`/info/moduleconfigtenant/info/${this.dataForm.moduleId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.moduleName = data.moduleConfigTenant.moduleName
                this.dataForm.parentId = data.moduleConfigTenant.parentId
                this.dataForm.moduleIcon = data.moduleConfigTenant.moduleIcon
                this.dataForm.moduleUrl = data.moduleConfigTenant.moduleUrl
                this.dataForm.status = data.moduleConfigTenant.status
                this.dataForm.createTime = data.moduleConfigTenant.createTime
                this.dataForm.updateTime = data.moduleConfigTenant.updateTime
                this.dataForm.tenantId = data.moduleConfigTenant.tenantId
                this.dataForm.moduleType = data.moduleConfigTenant.moduleType
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/info/moduleconfigtenant/${!this.dataForm.moduleId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'moduleId': this.dataForm.moduleId || undefined,
                'moduleName': this.dataForm.moduleName,
                'parentId': this.dataForm.parentId,
                'moduleIcon': this.dataForm.moduleIcon,
                'moduleUrl': this.dataForm.moduleUrl,
                'moduleType': this.dataForm.moduleType
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      changeRadio (e) {
        if (e === 1) {
          this.dataForm.moduleType = '2'
          this.moduleShow = false
        }
        if (e === 2) {
          this.dataForm.moduleType = '3'
          this.dataForm.parentId = 0
          this.moduleShow = true
        }
        if (e === 0) {
          this.dataForm.moduleType = '1'
          this.moduleShow = false
        }

      }
    }
  }
</script>
